<?php
class advertisingModel extends baseModel{
    protected $table = 'advertising';

    //广告位列表
    public function advertising_list() {
        return $this->select('','','id desc');
    }

    //广告位类型
    public function type_list($id=null) {
        $list=array(
            1=>'文字',
            2=>'链接',
            3=>'HTML',
            4=>'图片',
            5=>'多图',
            );
        if($id){
            return $list[$id];
        }else{
            return $list;
        }
    }

    //处理保存广告位
    public function format_data($type,$data) {
        if(!empty($data['start_time'])){
            $data['start_time']=strtotime($data['start_time']);
        }
        if(!empty($data['stop_time'])){
            $data['stop_time']=strtotime($data['stop_time']);
        }
        switch ($type) {
            case 1:
                $data['content']=in($data['content']);
                break;
            case 2:
                $data['content']= serialize(array('txt'=>$data['txt'],'url'=>$data['url']));
                break;
            case 3:
                $data['content']= html_in($data['content']);
                break;
            case 4:
                $data['content']= serialize(array('image'=>$data['image'],'url'=>$data['url']));
                break;
            case 5:
                $array=array();
                if(!empty($data['image'])){
                    foreach ($data['image'] as $key => $value) {
                        $array[$key]['image']=$value;
                        $array[$key]['url']=$data['url'][$key];
                        $array[$key]['txt']=$data['txt'][$key];
                        $array[$key]['order']=$data['order'][$key];
                    }
                }
                $data['content']= serialize($array);
                break;
            
            default:
                in($data);
                break;
        }
        return $data;
    }

    //获取广告位数据
    public function get_format_data($data) {
        switch ($data['type']) {
            case 1:
                $data['content']=$data['content'];
                $data['ads']=$data['content'];
                break;
            case 2:
                $array= unserialize($data['content']);
                if(!empty($array)){
                    foreach ($array as $key=>$value) {
                        $data[$key]=$value;
                        $data['ads'][$key]=$value;
                    }
                }
                break;
            case 3:
                $data['content']= html_out($data['content']);
                $data['ads']=html_out($data['content']);
                break;
            case 4:
                $array= unserialize($data['content']);
                if(!empty($array)){
                    foreach ($array as $key=>$value) {
                        $data[$key]=$value;
                        $data['ads'][$key]=$value;
                    }
                }
                break;
            case 5:
                $data['images_list']= unserialize($data['content']);
                $data['ads']=unserialize($data['content']);
                break;
            default:
                in($data);
                $data['ads']=$data['content'];
                break;
        }
        return $data;
    }

    //通过标识获取广告位信息
    public function info_label($sign) {
        $info=$this->find('sign="'.$sign.'"');
        if(empty($info)){
            return;
        }
        if($info['start_time']>time()){
            return;
        }
        if(!empty($info['stop_time'])&&$info['stop_time']<time()){
            return;
        }
        $info=$this->get_format_data($info);
        return $info['ads'];
    }

    //广告位信息
    public function info($id) {
        $info=$this->find('id='.$id);
        $info=$this->get_format_data($info);
        return $info;
    }

    //获取指定广告位列表
    public function content_list($id) {
        return $this->select('id in('.$id.')','','id desc');
    }

    //添加广告位
    public function add($data)
    {
        $type=intval($_POST['type']);
        $data=$this->format_data($type,$data);
        return $this->insert($data);
    }
    //编辑广告位
    public function edit($data)
    {
        $type=intval($data['type']);
        $data=$this->format_data($type,$data);
        $condition['id']=intval($data['id']);
        return $this->update($condition,$data); 
    }
    //删除广告位
    public function del($id)
    {
        return $this->delete('id='.intval($id)); 
    }

	
}